package thread;

import java.util.PriorityQueue;
import java.util.Timer;
import java.util.TimerTask;

//定时器的练习、使用及模拟
public class ThreadDemo16 {
    public static void test(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("延时3000毫秒");
            }
        }, 3000);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("延时2000毫秒");
            }
        }, 2000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("延时1000毫秒");
            }
        }, 1000);
    }

    public static void MyTimerTest(){
        MyTimer timer = new MyTimer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("延时3000毫秒");
            }
        }, 3000);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("延时2000毫秒");
            }
        }, 2000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("延时1000毫秒");
            }
        }, 1000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("延时2500毫秒");
            }
        }, 2500);
    }
    public static void main(String[] args) {
        //test();

        MyTimerTest();
    }
}

//定时器的执行任务
class MyTimerTask implements Comparable<MyTimerTask>{
    private Runnable runnable;  //执行任务
    private long time;  //延迟执行任务时间

    public Runnable getRunnable() {
        return runnable;
    }

    public long getTime() {
        return time;
    }
    //构造函数
    public MyTimerTask(Runnable runnable, long delay) {
        this.runnable = runnable;
        //当前时间 + 延迟时间，作为执行任务的时间戳
        this.time = System.currentTimeMillis() + delay;
    }
    //比较函数，用于优先级队列的任务比较
    @Override
    public int compareTo(MyTimerTask o) {
        return (int)(this.time - o.time);
    }
}

//定时器模拟
class MyTimer{
    //创建优先级队列，出队或者入队需要加锁
    private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();
    //创建锁对象
    private Object locker = new Object();

    public void schedule(Runnable runnable, long delay){
        //根据参数，创建任务，将任务插入队列，要加锁
        synchronized (locker){
            MyTimerTask task = new MyTimerTask(runnable, delay);
            queue.offer(task);
            locker.notifyAll(); //唤醒扫描线程
        }
    }

    //构造函数，创建一个扫描队列中的任务的线程
    public MyTimer(){
        Thread t = new Thread(()->{
            while(true){
                try{
                    //加锁
                    synchronized (locker){
                        //空队列，阻塞等待
                        while(queue.isEmpty()){
                            locker.wait();
                        }
                        //非空队列，获取队首元素
                        MyTimerTask task = queue.peek();
                        //获取当前时间戳
                        long curTime = System.currentTimeMillis();
                        //判断是否到了执行时间，到了时间则出队，并执行任务
                        if(curTime >= task.getTime()){
                            queue.poll();   //出队
                            task.getRunnable().run();   //执行任务
                        }else { //未到执行时间，继续阻塞等待
                            locker.wait(task.getTime() - curTime);
                        }
                    }
                }catch (InterruptedException e){
                    e.printStackTrace();
                }

            }
        });
        //启动线程
        t.start();
    }
}

